package cn.liutao.auth.api;

import cn.liutao.auth.entity.param.RegisterParam;
import cn.liutao.auth.service.AuthService;
import cn.liutao.common.utils.ServletUtils;
import cn.liutao.common.utils.ValidUtil;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.groups.Default;

/**
 * @author liutao
 * @Description 实现注册功能
 * @date 2023/10/30 19:33
 * @Version 1.0
 */
@WebServlet("/auth/register")
@Slf4j
public class RegisterServlet extends HttpServlet {

    private AuthService authService;
    /**
     * 初始化
     * @param config
     * @throws ServletException
     */
    @Override
    public void init(ServletConfig config) throws ServletException {
        authService = AuthService.getInstance();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp){
        RegisterParam registerParam= JSONObject.parseObject(cn.hutool.extra.servlet.ServletUtil.getBody(req), RegisterParam.class);
        //表单校验
        ValidUtil.validated(registerParam, Default.class);
        //校验验证码
        authService.verify(registerParam);
        //获取进行注册
        authService.register(registerParam);

        ServletUtils.ok(resp,"注册成功");
    }
}
